#include <graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<conio.h>

#define WIDTH 870 //屏幕宽度
#define HEIGHT 1200 //屏幕高度
#define PI 3.1415926 //π的值
#define LENTH 115 //初始线长
#define LENWIDTH 3 //线宽
#define SNOW_LENWIDTH 1

float def_angle = PI*0.7; //两侧偏移角度
float shortenRate = 0.83; //缩短速率
float star_x,star_y;
int colorMode=1;

void Fengxing_line(double x0, double y0, double angle, double diameter, int depth);
void Fengxing_draw(double x0, double y0, double angle, double diameter, int depth);

void draw_star(int height,int H,int S,int L,float x,float y)
{
	setbkmode(TRANSPARENT);
	TCHAR star[10];			
	_stprintf_s(star, _T("★")); //字符转化

	LOGFONT f;
	gettextstyle(&f);						// 获取当前字体设置
	f.lfHeight = height;						// 设置字体高度（大小)
	f.lfQuality = ANTIALIASED_QUALITY;		// 设置输出效果为抗锯齿
	_tcscpy(f.lfFaceName, _T("华文彩云"));	// 设置字体
	settextcolor(HSLtoRGB(H,S,L)); //设置字体颜色
	settextstyle(&f);						// 设置字体样式

	outtextxy(x,y,star);
}

void draw_light(float next_x,float next_y)
{
	float radius=5+rand()%5;
	int H=rand()%255,
		S=10,
		L=255;

	while(radius>=1)
	{
		setfillcolor(HSLtoRGB(H,S,L));
		setlinecolor(HSLtoRGB(H,S,L));
		
		fillcircle(next_x,next_y,radius);

		radius--;
		S--;
		L--;
	}
}

void draw_right(float x,float y,float lenth,float jiaodu,int zidai)
{
	setlinestyle(PS_SOLID,LENWIDTH); //调整直线绘制方式、宽度
	float next_x,next_y; //末端节点的x,y坐标
	next_x = x+ lenth* cos(jiaodu);
	next_y = y+ lenth* sin(jiaodu);

	//灯（？
	if(rand()%600==1)
	{
		draw_light(next_x,next_y);
	}
	//星
	if(rand()%600==1)
	{
		int H=rand()%255,
			S=255,
			L=255;
		int height=15+rand()%15;
		draw_star(height,H,S,L,next_x,next_y);
	}

	int R,G,B;
	if(colorMode==1){
		int Rt=255,Gt=255,Bt=150;//最终颜色

		R=10+zidai*1.5;
		G=160+zidai*4;
		B=10+zidai*15;
		if(R>=Rt){
			R=Rt;
		}
		if(G>=Gt){
			G=Gt;
		}
		if(B>=Bt){
			B=Bt;
		}
	}else if(colorMode==0){
		int Rt=255,Gt=253,Bt=152;//最终颜色

		R=244+zidai*1.5;
		G=199+zidai*4;
		B=10+zidai*15;
		if(R>=Rt){
			R=Rt;
		}
		if(G>=Gt){
			G=Gt;
		}
		if(B>=Bt){
			B=Bt;
		}
	}
	
	setlinecolor(RGB(R,G,B)); //调整直线颜色
	line(x,y,next_x,next_y); //绘制直线

	int childGeneration = zidai + 1; //子代数量

	float childLength = shortenRate*lenth; //子代长度

	float centerChildLength = childLength; //中心子代长度
	float leftChildLength = childLength*0.4; //左支子代长度
	float rightChildLength = childLength*0.4; //右支子代长度
	
	Sleep(1);

	if ( childLength>=0.5&&childGeneration<=100) //如果子代长度>=2且子代数量<=100
	{		
		draw_right(next_x,next_y,centerChildLength,jiaodu-PI/72,childGeneration); //绘制中心支
		draw_right(next_x,next_y,leftChildLength,jiaodu+def_angle,childGeneration); //绘制左支
		draw_right(next_x,next_y,rightChildLength,jiaodu-def_angle,childGeneration); //绘制右支
	}
}

void draw_left(float x,float y,float lenth,float jiaodu,int zidai)
{
	setlinestyle(PS_SOLID,LENWIDTH); //调整直线绘制方式、宽度
	float next_x,next_y; //末端节点的x,y坐标
	next_x = x+ lenth* cos(jiaodu);
	next_y = y+ lenth* sin(jiaodu);

	//灯（？
	if(rand()%600==1)
	{
		draw_light(next_x,next_y);
	}
	//星
	if(rand()%600==1)
	{
		int H=rand()%255,
			S=255,
			L=255;
		int height=5+rand()%5;
		draw_star(height,H,S,L,next_x,next_y);
	}

	int R,G,B;
	if(colorMode==1){
		int Rt=255,Gt=255,Bt=150;//最终颜色

		R=10+zidai*1.5;
		G=160+zidai*4;
		B=10+zidai*15;
		if(R>=Rt){
			R=Rt;
		}
		if(G>=Gt){
			G=Gt;
		}
		if(B>=Bt){
			B=Bt;
		}
	}else if(colorMode==0){
		int Rt=255,Gt=253,Bt=152;//最终颜色

		R=244+zidai*1.5;
		G=199+zidai*4;
		B=10+zidai*15;
		if(R>=Rt){
			R=Rt;
		}
		if(G>=Gt){
			G=Gt;
		}
		if(B>=Bt){
			B=Bt;
		}
	}
	
	setlinecolor(RGB(R,G,B)); //调整直线颜色
	line(x,y,next_x,next_y); //绘制直线

	int childGeneration = zidai + 1; //子代数量

	float childLength = shortenRate*lenth; //子代长度

	float centerChildLength = childLength; //中心子代长度
	float leftChildLength = childLength*0.5; //左支子代长度
	float rightChildLength = childLength*0.4; //右支子代长度
	
	Sleep(1);

	if ( childLength>=1&&childGeneration<=100) //如果子代长度>=1且子代数量<=100
	{		
		draw_left(next_x,next_y,centerChildLength,jiaodu+PI/72,childGeneration); //绘制中心支
		draw_left(next_x,next_y,leftChildLength,jiaodu+def_angle,childGeneration); //绘制左支
		draw_left(next_x,next_y,rightChildLength,jiaodu-def_angle,childGeneration); //绘制右支
	}
}

void draw(float x,float y,float lenth,float jiaodu,int zidai)
{
	setlinestyle(PS_SOLID,3*LENWIDTH); //调整直线绘制方式、宽度
	float next_x,next_y; //末端节点的x,y坐标
	next_x = x+ lenth* cos(jiaodu);
	next_y = y+ lenth* sin(jiaodu);


	int R,G,B;
	if(colorMode==1){
		int Rt=255,Gt=255,Bt=150;//最终颜色

		R=10+zidai*1.5;
		G=160+zidai*4;
		B=10+zidai*15;
		if(R>=Rt){
			R=Rt;
		}
		if(G>=Gt){
			G=Gt;
		}
		if(B>=Bt){
			B=Bt;
		}
	}else if(colorMode==0){
		int Rt=255,Gt=253,Bt=152;//最终颜色

		R=244+zidai*1.5;
		G=199+zidai*4;
		B=10+zidai*15;
		if(R>=Rt){
			R=Rt;
		}
		if(G>=Gt){
			G=Gt;
		}
		if(B>=Bt){
			B=Bt;
		}
	}
	
	setlinecolor(RGB(R,G,B)); //调整直线颜色
	line(x,y,next_x,next_y); //绘制直线

	int childGeneration = zidai + 1; //子代数量

	float childLength = shortenRate*lenth; //子代长度

	float centerChildLength = childLength*0.95; //中心子代长度
	float leftChildLength = childLength*0.5; //左支子代长度
	float rightChildLength = childLength*0.5; //右支子代长度
	
	star_x=next_x,star_y=next_y; //找到最后星星的坐标

	Sleep(1);
	if ( childLength>=2&&childGeneration<=100) //如果子代长度>=2且子代数量<=100
	{		
		draw(next_x,next_y,centerChildLength,jiaodu,childGeneration); //绘制中心支
		draw_right(next_x,next_y,leftChildLength,jiaodu+def_angle,childGeneration); //绘制左支
		draw_left(next_x,next_y,rightChildLength,jiaodu-def_angle,childGeneration); //绘制右支
	}
}

void Fengxing_draw(double x0, double y0, double angle, double diameter, int depth)
{
	double shuzhi_angle = angle + PI;
	double x1 = x0 + diameter * cos(angle);
	double y1 = y0 + diameter * sin(angle);
	double xie_angle = PI / 3;
	diameter /= (1 + 2 * cos(xie_angle));
	angle += xie_angle;
	Fengxing_line(x0, y0, angle, diameter, depth);
	x0 += diameter * cos(angle);
	y0 += diameter * sin(angle);
	Fengxing_line(x0, y0, angle - xie_angle, diameter, depth);
	Fengxing_line(x0, y0, angle + (PI - 2 * xie_angle), diameter, depth);
	angle -= xie_angle;
	x0 += diameter * cos(angle);
	y0 += diameter * sin(angle);
	Fengxing_line(x0, y0, angle - xie_angle, diameter, depth);
	Fengxing_line(x0, y0, angle + xie_angle, diameter, depth);
	angle -= xie_angle;
	Fengxing_line(x1, y1, shuzhi_angle, diameter * 2, depth);
}

void Fengxing_line(double x0, double y0, double angle, double diameter, int depth)
{
	if (depth == 0) {
		double x1 = x0 + diameter * cos(angle);
		double y1 = y0 + diameter * sin(angle);
		line((int)x0, (int)y0, (int)x1, (int)y1);
		Sleep(0);
	}
	else {
		depth--;
		diameter /= 2;
		Fengxing_line(x0, y0, angle, diameter / 2, depth);
		x0 += diameter * cos(angle) / 2;
		y0 += diameter * sin(angle) / 2;
		Fengxing_draw(x0, y0, angle, diameter, depth);
		x0 += diameter * cos(angle);
		y0 += diameter * sin(angle);
		Fengxing_draw(x0, y0, angle + PI, diameter, depth);
		Fengxing_line(x0, y0, angle, diameter / 2, depth);
	}
}

void snowflake(int x1,int x2,int y1,int y2,int amount)
{
	setlinestyle(PS_SOLID,SNOW_LENWIDTH); //调整直线绘制方式、宽度
	setlinecolor(WHITE);
	
	for(int i=0;i<amount;i++)
	{
		double diameter=5+rand()%20;
		int depth=1+rand()%3;
		float x=WIDTH;
		double angle=PI/2 + rand()%5;
		double x0 = -diameter * cos(angle) / 2 + x1+rand()%x2;
		double y0 = -diameter * sin(angle) / 2 + y1+rand()%y2;
		Fengxing_line(x0, y0, angle, diameter, depth);
	}
}

void draw_floor()
{
	for(int i=0;i<250;i++)
	{
		int H=rand()%255,
			S=255,
			L=255,
			x=rand()%WIDTH,
			y=(HEIGHT-300)+rand()%100,
			height=10+rand()%10;
		draw_star(height,H,S,L,x,y);
	}
	snowflake(0,WIDTH,HEIGHT-300,100,250);
}

void draw_text()
{
	LOGFONT f;
	gettextstyle(&f);						// 获取当前字体设置
	f.lfHeight = 130;						// 设置字体高度（大小)
	f.lfQuality = ANTIALIASED_QUALITY;		// 设置输出效果为抗锯齿
	_tcscpy(f.lfFaceName, _T("华文行楷"));	// 设置字体
	settextcolor(RGB(227,68,50)); //设置字体颜色
	settextstyle(&f);						// 设置字体样式
	
	outtextxy(100,1000,_T("Merry Christmas"));
	Sleep(500);
	gettextstyle(&f);						// 获取当前字体设置
	f.lfHeight = 70;						// 设置字体高度（大小)
	settextcolor(RGB(255,223,82)); //设置字体颜色
	settextstyle(&f);						// 设置字体样式
	
	outtextxy(300,1120,_T("2021.12.25"));
}

void update()
{
	MOUSEMSG m;
	if(MouseHit())//检测鼠标点击
	{
		m=GetMouseMsg();
		if(m.uMsg==WM_LBUTTONDOWN)//如果鼠标左键按下
		{
			cleardevice();

			draw(WIDTH/2,HEIGHT-300,LENTH,-PI/2,1);
			draw_star(60,60,1,100,star_x-30,star_y-48);

			//snowflake(,(HEIGHT-300)+rand()%100);
			//float x=rand()%WIDTH,y=rand()%(HEIGHT-380);

			snowflake(0,WIDTH,0,HEIGHT-380,150);
			draw_floor();
			draw_text();
		}
	}
}

void startup()//初始化函数
{
	initgraph(WIDTH,HEIGHT);//开窗
	setbkcolor(RGB(128,128,168));//设置背景色
	setlinestyle(PS_SOLID,LENWIDTH); //调整直线绘制方式、宽度
	cleardevice();//清屏
}

int main()
{
	startup();
	while(1)
		update();
	_getch();
}

